﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using XDL.Framework.Particles;
using XDL.Framework;

namespace JumpNFight.Simulation
{
    class Sliceable
    {
        protected static Random Rnd = new Random();
        EntityBase m_entity;
        ParticleSystem m_slicePS;
        public Vector2 Position;
        public float X
        {
            get
            {
                return Position.X;
            }
        }
        public float Y
        {
            get
            {
                return Position.Y;
            }
        }

        public Vector2 Velocity;
        public float VX
        {
            get
            {
                return Velocity.X;
            }
        }
        public float VY
        {
            get
            {
                return Velocity.Y;
            }
        }

        /// <summary>
        /// Returns the slice position
        /// </summary>
        /// <remarks>
        /// Starts from the bottom of the sprite
        /// </remarks>
        public int SliceHeight
        {
            get;
            private set;
        }

        public bool IsSliced
        {
            get;
            private set;

        }

        public Sliceable(EntityBase entity)
        {
            m_entity = entity;
            m_slicePS = GameServices.GetService<ParticleSystem>("Particles/DiscGunImpact");
        }

        public void Update(GameTime gameTime)
        {
            // nothing to do
            if (!IsSliced)
                return;

            float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

            Velocity.X += dt * m_entity.Moveable.Mass * World.Gravity.X;
            Velocity.Y += dt * m_entity.Moveable.Mass * World.Gravity.Y;
            Position.X += dt * Velocity.X;
            Position.Y += dt * Velocity.Y;
        }

        /// <summary>
        /// Slice the entity
        /// </summary>
        public void Slice(int sliceHeight)
        {
            for (int i = 0; i < m_entity.Moveable.Width; i++)
                m_slicePS.AddParticle(new Vector3(m_entity.Moveable.X - m_entity.Moveable.Width / 2 + i, m_entity.Moveable.Y + m_entity.Moveable.Height / 2 - sliceHeight, 0), new Vector3(m_entity.IsRunningRight ? -1 : 1, 0, 0));

            IsSliced = true;
            SliceHeight = sliceHeight;
            m_entity.Moveable.Velocity = new Vector2(Rnd.Next(-32, 32), -Rnd.Next(400, 450));
            Velocity = new Vector2(-m_entity.Moveable.VX, -Rnd.Next(400, 450));
            Position = m_entity.Moveable.Position;
        }

        public void Reset()
        {
            IsSliced = false;
        }
    }
}
